package com.yhsimon.singleton.type6;

public class SingletonTest6 {
    public static void main(String[] args) {
        System.out.println("双重检查");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println("i1与i2实例是否相等："+(instance1==instance2));
        System.out.println("i1与i2的哈希值是否相同："+(instance1.hashCode()==instance2.hashCode()));
        System.out.println("i1的哈希值："+instance1.hashCode());
        System.out.println("i2的哈希值："+instance2.hashCode());
    }
}

//双重检查  推荐使用
class Singleton{
    private static volatile Singleton instance;

    private Singleton(){}

    public static Singleton getInstance(){
       if (instance==null){
           synchronized (Singleton.class){
               if(instance==null){
                   instance=new Singleton();
               }
           }
       }
       return instance;
    }


}